package com.capgemini.gae.data;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.capgemini.gae.daos.PersistableObject;
import com.google.appengine.api.datastore.Key;

@Entity
public class Performance implements PersistableObject {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Key id;

	@Temporal(TemporalType.TIME)
	private Date result;

	private int ranking;
	private boolean worldRecord;

	private Key athlete;

	@ManyToOne
	private Tournament tournament;

	public Performance() {
	}

	public Performance(Date result, int ranking, boolean worldRecord,
			Tournament tournament) {
		super();
		this.result = result;
		this.ranking = ranking;
		this.worldRecord = worldRecord;
	}

	public Long getId() {
		if (id != null) {
			return id.getId();
		} else
			return null;
	}

	public void setId(Key id) {
		this.id = id;
	}

	public Key getKey() {
		return id;
	}

	public Date getResult() {
		return result;
	}

	public void setResult(Date result) {
		this.result = result;
	}

	public int getRanking() {
		return ranking;
	}

	public void setRanking(int ranking) {
		this.ranking = ranking;
	}

	public boolean isWorldRecord() {
		return worldRecord;
	}

	public void setWorldRecord(boolean worldRecord) {
		this.worldRecord = worldRecord;
	}

	public Key getAthlete() {
		return athlete;
	}

	public void setAthlete(Athlete athlete) {
		this.athlete = athlete.getKey();
	}

	public Tournament getTournament() {
		return tournament;
	}

	public void setTournament(Tournament tournament) {
		this.tournament = tournament;
	}

	@Override
	public Class getEntityClass() {
		return Performance.class;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((athlete == null) ? 0 : athlete.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ranking;
		result = prime * result
				+ ((this.result == null) ? 0 : this.result.hashCode());
		result = prime * result
				+ ((tournament == null) ? 0 : tournament.hashCode());
		result = prime * result + (worldRecord ? 1231 : 1237);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Performance other = (Performance) obj;

		if (athlete == null) {
			if (other.athlete != null)
				return false;
		} else if (!athlete.equals(other.athlete))
			return false;

		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (ranking != other.ranking)
			return false;
		if (result == null) {
			if (other.result != null)
				return false;
		} else if (!result.equals(other.result))
			return false;
		if (tournament == null) {
			if (other.tournament != null)
				return false;
		} else if (!tournament.equals(other.tournament))
			return false;
		if (worldRecord != other.worldRecord)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Performance [id=" + id + ", result=" + result + ", ranking="
				+ ranking + ", worldRecord=" + worldRecord + ", athlete="
				+ athlete + ", tournament=" + tournament + "]";
	}

}
